{% extends 'stocks/base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{% url 'strategy_dashboard' %}">策略仪表板</a></li>
                    <li class="breadcrumb-item active">买入信号列表</li>
                </ol>
            </nav>
            <h2>买入信号列表</h2>
            <p class="text-muted">筛选和查看所有买入信号</p>
        </div>
    </div>

    <!-- 筛选表单 -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">筛选条件</h5>
                </div>
                <div class="card-body">
                    <form method="get" class="row g-3">
                        <div class="col-md-3">
                            <label for="symbol" class="form-label">股票代码</label>
                            <input type="text" class="form-control" id="symbol" name="symbol" value="{{ symbol }}" placeholder="输入股票代码">
                        </div>
                        <div class="col-md-3">
                            <label for="strategy" class="form-label">策略</label>
                            <select class="form-select" id="strategy" name="strategy">
                                <option value="">所有策略</option>
                                {% for s in strategies %}
                                    <option value="{{ s.id }}" {% if strategy_id == s.id|stringformat:"s" %}selected{% endif %}>
                                        {{ s.name }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label for="min_strength" class="form-label">最小强度</label>
                            <input type="number" class="form-control" id="min_strength" name="min_strength" value="{{ min_strength }}" min="0" max="100" step="10">
                        </div>
                        <div class="col-md-2">
                            <label for="days" class="form-label">天数</label>
                            <select class="form-select" id="days" name="days">
                                <option value="7" {% if days == 7 %}selected{% endif %}>7天</option>
                                <option value="30" {% if days == 30 %}selected{% endif %}>30天</option>
                                <option value="90" {% if days == 90 %}selected{% endif %}>90天</option>
                                <option value="365" {% if days == 365 %}selected{% endif %}>1年</option>
                            </select>
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="fas fa-filter"></i> 筛选
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 信号列表 -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">买入信号</h5>
                    <span class="badge bg-primary">共 {{ page_obj.paginator.count }} 个信号</span>
                </div>
                <div class="card-body">
                    {% if page_obj %}
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>股票代码</th>
                                        <th>股票名称</th>
                                        <th>策略名称</th>
                                        <th>信号日期</th>
                                        <th>信号价格</th>
                                        <th>信号强度</th>
                                        <th>买入理由</th>
                                        <th>创建时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for signal in page_obj %}
                                        <tr>
                                            <td>{{ signal.stock_id }}</td>
                                            <td>{{ signal.stock_id }}</td>
                                            <td>{{ signal.strategy_id }}</td>
                                            <td>{{ signal.signal_date|date:"Y-m-d" }}</td>
                                            <td>¥{{ signal.signal_price|floatformat:2 }}</td>
                                            <td>
                                                <span class="badge {% if signal.signal_strength >= 80 %}bg-danger{% elif signal.signal_strength >= 60 %}bg-warning{% else %}bg-info{% endif %}">
                                                    {{ signal.signal_strength|floatformat:1 }}
                                                </span>
                                            </td>
                                            <td>{{ signal.reason }}</td>
                                            <td>{{ signal.created_at|date:"Y-m-d H:i" }}</td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 分页 -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="Page navigation">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if symbol %}&symbol={{ symbol }}{% endif %}{% if strategy_id %}&strategy={{ strategy_id }}{% endif %}{% if min_strength %}&min_strength={{ min_strength }}{% endif %}{% if days %}&days={{ days }}{% endif %}">
                                                上一页
                                            </a>
                                        </li>
                                    {% endif %}

                                    {% for num in page_obj.paginator.page_range %}
                                        {% if page_obj.number == num %}
                                            <li class="page-item active">
                                                <span class="page-link">{{ num }}</span>
                                            </li>
                                        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                            <li class="page-item">
                                                <a class="page-link" href="?page={{ num }}{% if symbol %}&symbol={{ symbol }}{% endif %}{% if strategy_id %}&strategy={{ strategy_id }}{% endif %}{% if min_strength %}&min_strength={{ min_strength }}{% endif %}{% if days %}&days={{ days }}{% endif %}">
                                                    {{ num }}
                                                </a>
                                            </li>
                                        {% endif %}
                                    {% endfor %}

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if symbol %}&symbol={{ symbol }}{% endif %}{% if strategy_id %}&strategy={{ strategy_id }}{% endif %}{% if min_strength %}&min_strength={{ min_strength }}{% endif %}{% if days %}&days={{ days }}{% endif %}">
                                                下一页
                                            </a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle"></i> 未找到符合条件的买入信号。
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-12">
            <div class="d-flex gap-2">
                <a href="{% url 'strategy_dashboard' %}" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> 返回仪表板
                </a>
                <a href="{% url 'strategy_list' %}" class="btn btn-outline-primary">
                    <i class="fas fa-list"></i> 查看策略列表
                </a>
            </div>
        </div>
    </div>
</div>
{% endblock %}
